package test;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密Hex输出
 * 
 * @author <a href="mailto:whz-work@163.com">汪洪州</a>
 * 
 */
public class MD5 {
	public static String compile(String planText) {
		StringBuilder ciperText = new StringBuilder();

		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			byte[] bytes = (messageDigest.digest(planText.getBytes(Charset.forName("UTF-8"))));
			for (byte b : bytes) {
				ciperText.append(Character.forDigit((b >> 4) & 0x0f, 16));
				ciperText.append(Character.forDigit(b & 0x0f, 16));
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return ciperText.toString();
	}
	
	public static String compile16(String planText){
		try {
			return compile(planText).substring(8, 24);
		} catch (Exception e) {
			return compile(planText);
		}
		
	}
}
